from curl_cffi import requests
from scrapy.http import HtmlResponse
from crawler.connection.tools import retry, joint_url
from scrapy.downloadermiddlewares.retry import RetryMiddleware
from crawler.utils.func_tools import get_tunnel_ip, get_distribute_count

ua_list = [
    "edge99",
    "edge101",
    "chrome99",
    "chrome100",
    "chrome101",
    "chrome104",
    "safari15_3",
    "safari15_5"
]


class CookiesMiddleware(RetryMiddleware):

    @retry(retry_times=4, interval=0)
    def update_request_info(self, request):
        ids = request.meta['task']['info']['ids']
        if request.meta.get('page'):
            pageNumber = request.meta['page']
        else:
            pageNumber = '1'
        headers = {
            'authority': 'www.selfridges.com',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'accept-language': 'zh-CN,zh;q=0.9',
            'api-key': 'sh5d2d2ph9rgbps3ntrshxqn',
            'cache-control': 'no-cache',
            'content-type': 'application/json; charset=utf-8',
            '$cookie': 'utag_chan={"channel":"","channel_set":"","channel_converted":false,"awc":""}; _gcl_au=1.1.1466564412.1675316877; _scid=0645c982-9695-4620-acf9-2fba8e338bce; _cs_c=0; mdLogger=false; kampyle_userid=c6e2-12d7-f390-196f-b5c2-6e77-6a61-0427; CONSENTMGR=consent:true%7Cts:1675419562304; COOKIE_NOTICE_SEEN=seen; SIGNUP_POPUP_SEEN=seen; NEWSLETTER_DISMISSED=true; _tt_enable_cookie=1; _ttp=IQ2Xwl7Kepqc6gm63aMIQXMwfoT; _fbp=fb.1.1676774747989.497596608; _pin_unauth=dWlkPVpqRTVPVGd6WVRZdE1UQmpOeTAwTUdVeExXSmpPV1V0WVRVMFpEbGpPVEUzT1dObA; _sctr=1|1676736000000; BIGipServer~S603887-RD2~Test_HTTPS_POOL=\\u0021A6ZIubfOorwV7U42mi3gewpiRYKC98YDm9sXcgtRmFgbXIkeS4eWWFM0rCPHPvStXXFq5UF6V0c/6BeMh+OrWn8YkLLyiCOx3WbK9eFO; WC_PERSISTENT=fRh3PdthRKmoelcGgZnY3wJKiE45fJ3XNWME%2FW37hYI%3D%3B2023-02-21+10%3A32%3A51.728_1675316872437-1346757_10052_3115117973%2C-1%2CGBP%2Ca9%2B1%2BLAt7x%2FYEUwM4ABqlJ1n4%2BcmN5r00KcBPJlGNKI6XV%2FZ6L9goV7G0jqgXWqHCFkFDHne23KfR08lS21Yuw%3D%3D_10052; _gid=GA1.2.62314074.1676975575; SF_COUNTRY_LANG=HK_zh; fita.sid.selfridges=ctHw2cFsiE8maDh-o6NE_YzYuy14cqOn; mmapi.p.uat=%7B%22WomensContent%22%3A%22Yes%22%2C%22MensContent%22%3A%22No%22%7D; Apache=10.77.3.197.1676982425087523; mmapi.e.mensVisitArray=%5B1676979912844%2C1677041093003%5D; mmapi.p.srv=%22prodfracgeu03%22; __tmbid=sg-1675316474-c5d3dad7e9274090a6237235f03cd7e0; kampyleUserSession=1677060623037; kampyleUserSessionsCount=27; _cs_mk=0.5727399112977656_1677062782089; mmapi.p.bid=%22prodfracgeu03%22; __cf_bm=NVwkg88gdazbt7TPF9NfcTRyLGsGAjskoshp0oSb7uo-1677063397-0-AcZ8OE4eHM5DT/9iI6M0xu1S64VTGw1cRlhtleQyL8Y86yjde2CGgatNklVnKR8wiwf/YI0Sgcr5BxkaQSJMFQXAbpqU7boeLQadrTVCg/bYWt3SCXyDlAG/lwX/elSV+qsVPh/coHdhtpwznkW23b9UOW7CAoC5ctiZZzPa2HDvBQPuzkLLQUdH2rGAhc0fOAaUvGxRDayCch0xsguNntM=; mmapi.e.womensVisitArray=%5B1676981815685%2C1676982350196%2C1676983982543%2C1677039029904%2C1677041370675%2C1677063391704%2C1677063665165%5D; mmapi.p.pd=%22F9QeXjD_aZmrKiBnHyA-JaGgfpV-FwGeplvvItVdFHI%3D%7CmgAAAApDH4sIAAAAAAAEAGNh-NZ5xv2L2AUtBua0okRGIQZGJ4bn6xz0-RiqN1tIHBG57fEy4AXTQ5bbHgxA8B8KGITKy8v1ilNz0ooyU9JTi_WS83OZGMV5GJhAqoCgZTsDw89NTGAaIsLA6AoAc9Rj120AAAA%3D%22; JSESSIONID=00008_PGXq5b--AhQvrz8jNldjl:-1; _ga=GA1.1.2072552414.1675316876; kampyleSessionPageCounter=6; _cs_id=5ac565a8-b9e4-ac14-fda7-48ae8974d67f.1675316877.18.1677063667.1677062390.1.1709480877020; _cs_s=4.0.0.1677065467151; _ga_R05V82D63H=GS1.1.1677060426.17.1.1677063717.0.0.0; _uetsid=1c102600b1d311ed8b186d748263f44e; _uetvid=24ef67b0a2bd11ed855af36e1ad47e12; utag_main=v_id:018610aabe10000cf0b15fda375c05075002b06d00bd0$_sn:11$_ss:0$_st:1677065518770$dc_visit:10$_pn:12%3Bexp-session$ses_id:1677060416895%3Bexp-session$dc_event:12%3Bexp-session$dc_region:ap-east-1%3Bexp-session',
            'newrelic': 'eyJ2IjpbMCwxXSwiZCI6eyJ0eSI6IkJyb3dzZXIiLCJhYyI6IjI0OTMxNDAiLCJhcCI6IjQxMTk3MzE0OSIsImlkIjoiZDdiZTNiMGI2NTBmYzJkMSIsInRyIjoiMjA5YzE2N2Q4NWE2OTQ5OGVlM2E4MmNlYzYzYjg1ZDAiLCJ0aSI6MTY3NzA2MzcyNjIwMywidGsiOiIxMzIyODQwIn19',
            'referer': 'https://www.selfridges.com/HK/zh/cat/womens/clothing/dresses/?pn=2',
            'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"macOS"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'traceparent': '00-209c167d85a69498ee3a82cec63b85d0-d7be3b0b650fc2d1-01',
            'tracestate': '1322840@nr=0-1-2493140-411973149-d7be3b0b650fc2d1----1677063726203',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
            'x-requested-with': 'XMLHttpRequest',
        }
        impersonate = get_distribute_count('impersonate_count', ua_list)
        if 'https://www.baidu.com' in request.url:
            params = (
                ('ids', ids),
                ('pageNumber', pageNumber),
                ('pageSize', '60'),
            )
            url = 'https://www.selfridges.com/api/cms/ecom/v1/HK/en/productview/byCategory/byIds'
            url = joint_url(url, params)
            response = requests.get(url=url, headers=headers, proxies=get_tunnel_ip(), timeout=(30, 30),
                                    verify=False, impersonate=impersonate)
            response = HtmlResponse(url, body=response.text, encoding='utf-8', request=request)
            return response
        else:
            response = requests.get(request.url, headers=headers, proxies=get_tunnel_ip(), timeout=(30, 30),
                                    verify=False, impersonate=impersonate)
            response = HtmlResponse(request.url, body=response.text, encoding='utf-8', request=request)
            return response

    def process_request(self, request, spider):
        response = self.update_request_info(request)
        return response

    def process_response(self, request, response, spider):
        if "Developer Inactive" in response.text or 'Error code 502' in response.text:
            return self._retry(request, 'Error Request', spider)
        return response
